package cn.lena.idle.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import java.util.LinkedList;

import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.User;
import cn.lena.idle.util.CurrencySQL;

/**
 * @author lena
 * @date 2021/6/10
 */
public class UserDbHelper extends SQLiteOpenHelper {

    // 定义数据库表名
    public static final String DB_NAME = "tb_user";
    // 创建用户信息表
    private static final String CREATE_USER_DB = "create table "+DB_NAME+" (" +
            "id integer primary key autoincrement," +
            "uuid varchar(12)," +
            "username varchar(20)," +
            "password varchar(20)," +
            "head blob," +
            "phone varchar(11) default '17827414719'," +
            "address varchar(100) )";
    // 默认存在用户
    //private static final String DEFAULT_DATA ="insert into tb_user(id,uuid,username,password,address) values(1,'201810214719','lena','123456','南区D-1154')";


    /**
     * 修改用户信息 t
     */
    public boolean update(User user){
        SQLiteDatabase db=this.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("phone",user.getPhone());
        values.put("address",user.getAddress());
        int result=db.update(DB_NAME,values,"id = ?",new String[]{LoginUser.CURRENT_USER.getId().toString()});
        System.out.println("user====>"+LoginUser.CURRENT_USER);
        if(result == 1){
            // 更新缓存中数据
            LoginUser.CURRENT_USER=user;
            return true;
        }
        return false;
    }

    /**
     * 修改用户信息 t
     */
    public boolean update(String phone,String address,byte[] head){
        SQLiteDatabase db=this.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("phone",phone);
        values.put("address",address);
        values.put("head",head);
        int result=db.update(DB_NAME,values,"id = ?",new String[]{LoginUser.CURRENT_USER.getId().toString()});
        System.out.println("user====>"+LoginUser.CURRENT_USER);
        if(result == 1){
            // 更新缓存中数据
            LoginUser.CURRENT_USER.setPhone(phone);
            LoginUser.CURRENT_USER.setAddress(address);
            return true;
        }
        return false;
    }

    /**
     * 注册 t
     * @param user 学生用户
     */
    public int addUser(User user) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("uuid",user.getUuid());
        values.put("username",user.getUsername());
        values.put("password",user.getPassword());
        values.put("address",user.getAddress());
        values.put("phone",user.getPhone());
        // 头像不为空的时候才添加头像
        if(user.getHead() != null) {
            values.put("head",user.getHead());
        }
        // 返回值是当前id
        int result = (int)db.insert(DB_NAME, null, values);
        // 保存当前登录用户信息
        LoginUser.CURRENT_USER=user;
        LoginUser.CURRENT_USER.setId(result);
        return result;
    }

    /**
     * 登录 t
     * @param userid 学号
     * @param password 学生密码
     * @return
     */
    public String login(String userid,String password){
        SQLiteDatabase db=this.getWritableDatabase();
        String sql = "select * from tb_user where uuid = ? and password = ?";
        Cursor cursor=db.rawQuery(sql,new String[]{userid,password});
        // cursor.moveToNext()：指向下一行，若无则返回false
        if(cursor.moveToNext()) {
            User u=new User();
            u.setId(cursor.getInt(0));
            u.setUuid(userid);
            u.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            u.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            u.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            u.setHead(cursor.getBlob(cursor.getColumnIndex("head")));

            // 保存当前登录用户信息
            LoginUser.CURRENT_USER=u;

            return cursor.getString(cursor.getColumnIndex("username"));
        }
        return "";
    }


    /**
     * 修改密码 t
     */
    public int updatePassword(String password) {
        SQLiteDatabase db = this.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put("password",password);
        int result=db.update(DB_NAME,values,"id = ?",new String[]{LoginUser.CURRENT_USER.getId().toString()});
        return result;
    }


    public UserDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER_DB);
      //  db.execSQL(DEFAULT_DATA);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(CurrencySQL.DROP_SQL + DB_NAME);
        db.execSQL(CREATE_USER_DB);
       // db.execSQL(DEFAULT_DATA);
    }


}
